package com.me.generateXML;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AutoGenerate {
	private List<Table> list = new ArrayList<>();
	private String suffix = "Mapper";
	private String fileName = "table.xml";

	public AutoGenerate(String driver, String connectionURL, String username, String password) {
		try {
			Class.forName(driver);
			Connection con = DriverManager.getConnection(connectionURL, username, password);
			DatabaseMetaData dbmd = con.getMetaData();
			ResultSet tableSet = dbmd.getTables(null, "%", "%", new String[] { "Table" });
			while (tableSet.next()) {
				Table table = new Table(tableSet.getString("TABLE_NAME"));
				list.add(table);
			}
			tableSet.close();
			con.close();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void generateXML() {
		StringBuffer outStr = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		outStr.append("<tables>");
		BufferedWriter bf = null;
		File file = new File(fileName);
		for (Table table : list) {
			table.setMapperName(getMapperName(table.getTableName()));
			outStr.append(table);
		}
		outStr.append("</tables>");
		try {
			FileWriter out = new FileWriter(file);
			bf = new BufferedWriter(out);
			bf.write(outStr.toString());
			bf.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (bf != null) {
				try {
					bf.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	private String getMapperName(String tableName) {
		StringBuffer sbf = new StringBuffer();
		String[] strs = tableName.split("_");
		for (String str : strs) {
			sbf.append(toFirsetUpperCase(str));
		}
		sbf.append(suffix);
		return sbf.toString();
	}

	private String toFirsetUpperCase(String str) {
		// TODO Auto-generated method stub
		char[] c = str.toCharArray();
		if (c.length > 0) {
			if (c[0] >= 97) {
				c[0] = (char) (c[0] - 32);
			}
		}
		return new String(c);
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public void setEnableCountByExample(boolean enableCountByExample) {
		for (Table table : list) {
			table.setEnableCountByExample(enableCountByExample);
		}
	}

	public void setEnableUpdateByExample(boolean enableUpdateByExample) {
		for (Table table : list) {
			table.setEnableUpdateByExample(enableUpdateByExample);
		}
	}

	public void setEnableDeleteByExample(boolean enableDeleteByExample) {
		for (Table table : list) {
			table.setEnableDeleteByExample(enableDeleteByExample);
		}
	}

	public void setEnableSelectByExample(boolean enableSelectByExample) {
		for (Table table : list) {
			table.setEnableSelectByExample(enableSelectByExample);
		}
	}

	public void setSelectByExampleQueryId(boolean selectByExampleQueryId) {
		for (Table table : list) {
			table.setSelectByExampleQueryId(selectByExampleQueryId);
		}
	}

	public static void main(String args[]) {
		String driver = "com.mysql.jdbc.Driver";
		String connectionURL = "jdbc:mysql://localhost/vlink_hdj?useUnicode=true";
		String username = "root";
		String password = "root";
		AutoGenerate autoGenerate = new AutoGenerate(driver, connectionURL, username, password);
		autoGenerate.setSuffix("Dao");
		autoGenerate.generateXML();
	}

}
